import { createRouter, createWebHistory } from 'vue-router'
const routes = [
  {
    path: '/',
    component: () => import('@/views/OnLayOut'),
    redirect: '/categoryRe',
    beforeEnter: (to, from, next) => {
      if (to.path == '/categoryRe' || from.path == '/') {
        if (sessionStorage.getItem('TOKEN')) {
          next();
        } else {
          next('/login');
        }
      }
    },
    children: [
      {
        path: 'categoryRe',
        component: () => import('@/views/OnLayOut/CategoryRe'),
        meta: { title: '大众图书-分类管理' },
      },
      {
        path: 'booksRe',
        component: () => import('@/views/OnLayOut/BooksRe'),
        meta: { title: '大众图书-图书管理' },
      },
      {
        path: 'userRe',
        component: () => import('@/views/OnLayOut/UserRe'),
        meta: { title: '大众图书-用户管理' },
      },
      {
        path: 'link',
        component: () => import('@/views/OnLayOut/Link'),
        meta: { title: '大众图书-友情链接' },
      },
      {
        path: 'lendAndReturn',
        component: () => import('@/views/OnLayOut/LendAndReturn'),
        meta: { title: '大众图书-借阅与归还' },
      },
      {
        path: 'bookRecord',
        component: () => import('@/views/OnLayOut/BookRecord'),
        meta: { title: '大众图书-借阅记录' },
      },
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/UserLogin'),
    meta: { title: '大众图书-登录' },
    beforeEnter: (to, from, next) => {
      if (to.path == '/login') {
        //是否登录
        if (sessionStorage.getItem('TOKEN')) {
          next('/');
        } else {
          next();
        }
      }
    }
  },
]



const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})


//设置标题
router.afterEach((to, from) => {
  window.document.title = to.meta.title || '大众图书管理系统';
})

export default router
